package com.qubaopen.survey.web;

import java.io.BufferedReader;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.qubaopen.core.utils.AppUtil;
import com.qubaopen.core.utils.JSONResponseUtil;
import com.qubaopen.survey.service.UserService;
import com.qubaopen.survey.util.StringUtil;

@Controller
@RequestMapping("/user")
public class UserController {
	@Autowired
	UserService userService;

	@RequestMapping("/authenticate")
	public void sendAreas(HttpServletRequest request,
			HttpServletResponse response, HttpSession session) {
		JSONObject errObj = new JSONObject();
		try {
			System.out.println("aaaaaaaaa");
			JSONObject requestObj = StringUtil
					.getJSONObjectFromRequest(request);
			String username = requestObj.getString("username");
			String password = requestObj.getString("password");

			JSONResponseUtil.buildJSONObjectResponse(response,
					userService.authenticate(username, password));
		} catch (Exception e) {
			errObj.put("result", "0");
			errObj.put("err", "user authenticate fail!");
			JSONResponseUtil.buildJSONObjectResponse(response, errObj);
		}
	}

}
